const roleModel = require('../model/roleModel.js');
const permModel = require('../model/permModel.js');

let rolePerms = [];

module.exports.getAllRoles = async (req,res) =>{
	let result = await roleModel.getAllRoles();
	res.send({
		data: result,
		meta: {
			msg:'角色列表获取成功',
			status: 200
		}
	})
}
//生成角色以及对应的权限
module.exports.getAllRolePerm = async (req,res) =>{
	//清空数组
	rolePerms = [];
	//1.获得所有角色
	let roles = await roleModel.getAllRoles();
	//2.遍历所有角色  把角色数据添加到一个数组中
	for(i in roles){
		let role = roles[i];
		rolePerms.push({
			"id": role.role_id,
			"roleName": role.role_name,
			"roleDesc": role.role_desc,
			"children": []
		})
	}
	//3.获得所有权限  遍历角色数组  把角色对应的一级权限添加到children里边
	let permes = await permModel.getAllPerm();
	for(i in rolePerms){
		let rolep = rolePerms[i];
		//获取角色id
		let rid = rolep.id;
		if(rid !== 0){
			//获得角色的权限列表
			let psids = await roleModel.getPsidsByRid(rid);
			psids = psids[0].ps_ids.split(',');
			createMenuTree(permes,psids,rid,i)
		}else{
			createMenuTree(permes,null,rid,i)
		}
	}
	res.send({
		data: rolePerms,
		meta: {
			msg: '角色权限获取成功',
			status:200
		}
	})
}
//生成三级菜单树
/* 
  @param {
	 permes: 所有权限
	 psids: 角色对应的权限id列表
	 rid: 角色id
	 i: rolePerms角色权限数组下标
  }
 */
createMenuTree = (permes,psids,rid,idx) =>{
	//1.把角色对应的一级权限添加到children里边
	for( i in permes){
		let perm = permes[i];
		if(perm.ps_level == 0){
			if( rid !== 0){
				if(psids.indexOf(perm.ps_id + '') < 0) continue
			}
			rolePerms[idx].children.push({
				"id": perm.ps_id,
				"authName": perm.ps_name,
				"path": perm.ps_api_path,
				"children": []
			})
		}
	}
	//2.遍历角色的一级权限  嵌套遍历所有权限把一级权限对应的二级权限过滤出，添加到一级权限的children里
	for(i in rolePerms[idx].children){
		let rolec = rolePerms[idx].children[i];
		for(j in permes ){
			let perm = permes[j];
			if(perm.ps_level == 1 && perm.ps_pid == rolec.id){
				if( rid !== 0){
					if(psids.indexOf(perm.ps_id + '') < 0) continue
				}
				rolePerms[idx].children[i].children.push({
					"id": perm.ps_id,
					"authName": perm.ps_name,
					"path": perm.ps_api_path,
					"children": []
				})
			}
		}
	}
	//3.遍历角色 嵌套遍历角色一级权限  嵌套遍历角色二级权限，嵌套遍历所有权限，
	//  把二级权限对应的三级权限过滤出，添加到二级权限的children里
	for( i in rolePerms[idx].children ){
		let permOne = rolePerms[idx].children[i];
		for( j in permOne.children){
			let permTwo = permOne.children[j];
			for( id in permes){
				let perm = permes[id];
				if(perm.ps_level == 2 && perm.ps_pid == permTwo.id){
					if( rid !== 0){
						if(psids.indexOf(perm.ps_id + '') < 0) continue
					}
					rolePerms[idx].children[i].children[j].children.push({
						"id": perm.ps_id,
						"authName": perm.ps_name,
						"path": perm.ps_api_path,
					})
				}
			}
		}
	}
}
//删掉角色的某个权限
module.exports.delOnePerm = async (req,res) =>{
	let {rid,pid} = req.params;
	//1. 获得角色当前的权限id列表
	let psids = await roleModel.getPsidsByRid(rid);
	psids = psids[0].ps_ids.split(',');
	//2. 把对应权限id从权限id列表中移除
	let index = psids.indexOf(pid + '');
	psids.splice(index,1);
	let psidstr = psids.toString();
	//3. 把新的权限id列表存到数据库中
	let permInfo = [psidstr,rid];
	let result = await roleModel.delOnePerm(permInfo);
	if(result.affectedRows >0){
		//获得角色新的权限信息，返回给前端
		let permes = await permModel.getAllPerm();
		let permTree =  createThreeTree(permes,psids,rid);
		res.send({
			data: permTree,
			meta:{
				msg:'权限修改成功',
				status: 200
			}
		})
	}else{
		res.send({
			meta:{
				msg:'权限修改失败',
				status: 500
			}
		})
	}
}	
//生成三级权限的菜单
createThreeTree= (permes,psids,rid) =>{
	let permTree = [];
	//1.把角色对应的一级权限添加到children里边
	for( i in permes){
		let perm = permes[i];
		if(perm.ps_level == 0){
			if( rid !== 0){
				if(psids.indexOf(perm.ps_id + '') < 0) continue
			}
			permTree.push({
				"id": perm.ps_id,
				"authName": perm.ps_name,
				"path": perm.ps_api_path,
				"children": []
			})
		}
	}
	//2.遍历角色的一级权限  嵌套遍历所有权限把一级权限对应的二级权限过滤出，添加到一级权限的children里
	for(i in permTree){
		let rolec = permTree[i];
		for(j in permes ){
			let perm = permes[j];
			if(perm.ps_level == 1 && perm.ps_pid == rolec.id){
				if( rid !== 0){
					if(psids.indexOf(perm.ps_id + '') < 0) continue
				}
				permTree[i].children.push({
					"id": perm.ps_id,
					"authName": perm.ps_name,
					"path": perm.ps_api_path,
					"children": []
				})
			}
		}
	}
	//3.遍历角色 嵌套遍历角色一级权限  嵌套遍历角色二级权限，嵌套遍历所有权限，
	//  把二级权限对应的三级权限过滤出，添加到二级权限的children里
	for( i in permTree){
		let permOne = permTree[i];
		for( j in permOne.children){
			let permTwo = permOne.children[j];
			for( id in permes){
				let perm = permes[id];
				if(perm.ps_level == 2 && perm.ps_pid == permTwo.id){
					if( rid !== 0){
						if(psids.indexOf(perm.ps_id + '') < 0) continue
					}
					permTree[i].children[j].children.push({
						"id": perm.ps_id,
						"authName": perm.ps_name,
						"path": perm.ps_api_path,
					})
				}
			}
		}
	}

	return permTree;
}

module.exports.getPermByRid = async (req,res) =>{
	let permes = await permModel.getAllPerm();
	let permTree = createThreeTree(permes,null,0)
	res.send({
		data: permTree,
		meta: {
			msg: '角色权限获取成功',
			status: 200
		}
	})
}

module.exports.updatePerm = async (req,res) =>{
	let {rid} = req.params;
	let {psids} = req.body;
	// 定义给sql语句传参的数组
	let permInfo = [psids,rid];
	let result =  await roleModel.updatePerm(permInfo);
	if(result.affectedRows >0){
		res.send({
			meta:{
				msg:'角色权限修改成功',
				status:200
			}
		})
	}else{
		res.send({
			meta:{
				msg:'角色权限修改失败',
				status:500
			}
		})
	}
}